package com.alibaba.algo.controller.policy;

import com.alibaba.algo.annotation.OperateLogAnnotation;
import com.alibaba.algo.apiimpl.policy.SysPolicyService;
import com.alibaba.algo.core.exception.BaseException;
import com.alibaba.algo.enums.OperateTypeEnum;
import com.alibaba.algo.enums.ResourceTypeEnum;
import com.alibaba.algo.exception.SysPolicyErrorCode;
import com.aliyun.gsuite.api.IClientForPolicy;
import com.aliyun.gsuite.model.CapPolicyInfo;
import com.aliyun.gsuite.model.CapRoleInfo;
import com.aliyun.gsuite.model.CapUserPolicyInfo;
import com.aliyun.gsuite.request.*;
import com.aliyun.gsuite.request.batch.BatchAddAndAttachPolicyVO;
import com.aliyun.gsuite.request.batch.BatchDeletePolicyVO;
import com.aliyun.gsuite.request.batch.BatchListRolesByPolicyVO;
import com.aliyun.gsuite.response.BaseResult;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SysPolicyController implements IClientForPolicy {

    @Autowired
    SysPolicyService sysPolicyService;

    @Override
    public BaseResult<Map<String, Boolean>> batchCheckPolicy(BatchCheckPolicyVO batchCheckPolicyVO) {
        Map<String, Boolean> datas = new HashMap<>();
        try {
            datas = sysPolicyService.batchCheckPolicy(batchCheckPolicyVO);
        } catch (BaseException ex) {
            return BaseResult.result(SysPolicyErrorCode.valueOf(ex.getErrorCode()).name(), ex.getErrorMsg());
        }
        return BaseResult.success(datas);
    }

    @Override
    public BaseResult<Boolean> checkPolicy(CheckPolicyVO checkPolicyVO) {
        boolean checkResult = false;
        try {
            checkResult = sysPolicyService.checkPolicy(checkPolicyVO);
        } catch (BaseException ex) {
            return BaseResult.result(SysPolicyErrorCode.valueOf(ex.getErrorCode()).name(), ex.getErrorMsg());
        }
        return BaseResult.success(checkResult);
    }


    @Override
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.FUNCTION_INFO,operateType = OperateTypeEnum.INSERT)
    public BaseResult addPolicy(AddPolicyVO addPolicyVO) {
        //后续添加权限直接在用户组件中做操作，此接口不再作为添加权限入口
        return BaseResult.success();
    }

    @Override
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.FUNCTION_INFO,operateType = OperateTypeEnum.INSERT)
    public BaseResult attachPolicy(AttachPolicyVO attachPolicyVO) {
        //后续attach权限直接在用户组件中做操作，此接口不再作为添加权限入口
        return BaseResult.success();
    }

    @Override
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.FUNCTION_INFO,operateType = OperateTypeEnum.INSERT)
    public BaseResult batchAddAndAttachPolicy(BatchAddAndAttachPolicyVO batchAddAndAttachPolicyVO) {
        //后续添加权限直接在用户组件中做操作，此接口不再作为添加权限入口
        return BaseResult.success();
    }

    @Override
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.FUNCTION_INFO,operateType = OperateTypeEnum.DELETE)
    public BaseResult<Map<String, BaseResult>> batchDeletePolicy(BatchDeletePolicyVO batchDeletePolicyVO) {
        //后续删除权限直接在用户组件中做操作，此接口不再作为添加权限入口
        return BaseResult.success();
    }

    @Override
    //该方法不是强依赖，暂不实现
    public BaseResult<Page<CapPolicyInfo>> list(ListPolicyVO listPolicyVO) {
        return null;
    }


    @Override
    //该方法不是强依赖，暂不实现
    public BaseResult<List<CapUserPolicyInfo>> listUserPolicy(ListUserPolicyVO listUserPolicyVO) {
        return null;
    }

    @Override
    //该方法不是强依赖，暂不实现
    public BaseResult<List<CapRoleInfo>> listRolesByPolicy(ListRolesByPolicyVO listUserPolicyVO) {
        return null;
    }


    @Override
    public BaseResult<Map<String, List<CapRoleInfo>>> batchListRolesByPolicy(BatchListRolesByPolicyVO batchListRolesByPolicyVO) {
        //不再给门户组件返回用户列表
        return BaseResult.success();
    }
}
